﻿using System;

namespace TodoCore
{
    public class Task
    {

        #region Member variables

        private string m_OutlookAppointmentID = "";
        private bool m_bComplete;
        private Decimal _mDTimeSpent = 5;
        private int _mIParentTaskId;
        private Int16 m_iPercentComplete;
        private Int16 m_iPosition;
        private Int16 m_iPriority = 5;
        private Int64 m_iPriorityColour = 5;
        private Int16 m_iRisk = 5;
        private int m_iTaskId;
        private Int16 m_iTimeEstimate = 5;

        private string m_strCost;
        private string m_strDependancy;
        private string m_strExternalId;
        private string m_strFileLink;
        private string m_strFlag;

        #region Times

        private DateTime m_dtCompletedDate;

        private DateTime m_dtCreationDate;

        private DateTime m_dtDoneDate;
        private DateTime m_dtDueDate;

        private DateTime m_dtLastMod;
        private DateTime m_dtStartDate;
        private string m_sTimeEstUnits = "H";
        private string m_sTimeSpentUnits = "H";
        private string m_strCompletedDate;
        private string m_strCreationDate;
        private string m_strDoneDate;
        private string m_strDueDate;
        private string m_strStartDate;
        private string m_strm_dtLastMod;

        #endregion

        #endregion

        #region Properties

        public String AllocatedBy { get; set; }

        public String AllocatedTo { get; set; }

        public String AllocatedTo1 { get; set; }

        public String Category { get; set; }

        public Int64 Colour { get; set; }

        public String CommentsType { get; set; }

        public Boolean Complete
        {
            get { return m_bComplete; }
            set
            {
                m_bComplete = value;
                if (value)
                {
                    m_iPercentComplete = 100;
                }
                else
                {
                    m_iPercentComplete = 0;
                }
            }
        }

        public String CreatedBy { get; set; }

        public DateTime CreationDate
        {
            get { return m_dtCreationDate; }
            set
            {
                m_dtCreationDate = value;
                m_strCreationDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public DateTime DoneDate
        {
            get { return m_dtDoneDate; }
            set
            {
                m_dtDoneDate = value;
                m_strDoneDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string DoneDateStr
        {
            get { return m_dtDoneDate.ToString("yyyy-mm-dd"); }
        }

        public DateTime DueDate
        {
            get { return m_dtDueDate; }
            set
            {
                m_dtDueDate = value;
                m_strDueDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string DueDateStr
        {
            get { return m_dtDueDate.ToString("yyyy-mm-dd"); }
        }

        public String FileRefPath { get; set; }

        public Single HoursEst { get; set; }

        public String CommentsStr { get; set; }

        public int ParentTaskId
        {
            get { return _mIParentTaskId; }
            set
            {
                if (value >= 0 && value <= 16384)
                {
                    _mIParentTaskId = value;
                }
            }
        }

        public Int16 PercentComplete
        {
            get { return m_iPercentComplete; }
            set
            {
                if (value >= 0 && value <= 100)
                {
                    m_iPercentComplete = value;
                    if (value == 100)
                    {
                        m_bComplete = true;
                    }
                    else
                    {
                        m_bComplete = false;
                    }
                }
            }
        }

        public Int16 Position
        {
            get { return m_iPosition; }
            set
            {
                if (value >= 0 && value <= 100)
                {
                    m_iPosition = value;
                }
            }
        }

        public Int16 Priority
        {
            get { return m_iPriority; }
            set
            {
                if (value >= 0 && value <= 10)
                {
                    m_iPriority = value;
                }
            }
        }

        public Int64 PriorityColour
        {
            get { return m_iPriorityColour; }
            set { m_iPriorityColour = value; }
        }

        public string PriorityWebColour { get; set; }

        public Int16 Risk
        {
            get { return m_iRisk; }
            set { m_iRisk = value; }
        }

        public string RtfComments { get; set; }

        public DateTime StartDate
        {
            get { return m_dtStartDate; }
            set
            {
                m_dtStartDate = value;
                m_strStartDate = FormatDateString(value.ToString("yyyy-mm-dd"));
            }
        }

        public string StartDateStr
        {
            get { return m_dtStartDate.ToString("yyyy-mm-dd"); }
        }

        public String Status { get; set; }

        public int TaskId
        {
            get { return m_iTaskId; }
            set
            {
                if (value >= 0 && value <= 16384)
                {
                    m_iTaskId = value;
                }
            }
        }

        public String TextColour { get; set; }

        public String TextWebColour { get; set; }

        public String TimeEstUnits { get; set; }

        public String TimeESpentUnits { get; set; }

        public Decimal TimeSpent
        {
            get { return _mDTimeSpent; }
            set { _mDTimeSpent = value; }
        }

        public String Title { get; set; }

        public String VersionStr { get; set; }

        public String WebColour { get; set; }

        public DateTime LastMod
        {
            get { return m_dtLastMod; }
            set
            {
                m_dtLastMod = value;
                m_strm_dtLastMod = FormatDateString(value.ToString("yyyy-mm-dd"));

            }
        }

        public string OutlookAppointmentID
        {
            get { return m_OutlookAppointmentID; }
            set { m_OutlookAppointmentID = value; }
        }

        public bool OutlookAppointmentEnabled { get; set; }

        #endregion

        private static String FormatDateString(String strDate)
        {
            String strReturn;

            try
            {
                strReturn = Convert.ToDateTime(strDate).ToString("MM/dd/yyyy");
            }
            catch
            {
                strReturn = strDate;
            }

            return strReturn;
        }
    }

}
